package com.gdones.wms.bus.classification.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.common.core.util.MyPinYinUtil;
import com.gdones.wms.bus.classification.dto.WmsClassificationModel;
import com.gdones.wms.bus.classification.mapper.WmsClassificationModelMapper;
import com.gdones.wms.bus.classification.service.IWmsClassificationModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 物资分类下的物资型号 服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-05-04
 */
@Service
public class WmsClassificationModelServiceImpl extends ServiceImpl<WmsClassificationModelMapper, WmsClassificationModel> implements IWmsClassificationModelService {

    @Override
    public MyResult pages(WmsClassificationModel classificationModel, MyPageBand pageBand) {

        MyResult result = new MyResult();

        WmsClassificationModelMapper modelMapper = getBaseMapper();

        IPage<WmsClassificationModel> pages = modelMapper.myPage(PageDTO.of(pageBand.getPage(),pageBand.getSize()),classificationModel);

        // 根据分页的结果封装返回数据
        MyPager<WmsClassificationModel> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(pageBand.getPage());
        pager.setPageSize(pageBand.getSize());
        pager.setRows(pages.getRecords());

        result.setMsg("查询成功！");
        result.setData(pager);

        return result;
    }

    @Override
    public MyResult myList(WmsClassificationModel classificationModel) {

        MyResult result = new MyResult();

        WmsClassificationModelMapper modelMapper = getBaseMapper();

        List<WmsClassificationModel> list = modelMapper.myList(classificationModel);

        result.setMsg("查询成功！");
        result.setData(list);

        return result;
    }

    @Override
    public MyResult add(WmsClassificationModel classificationModel) {

        MyResult result = new MyResult();

        String code = "MMODEL-" + MyPinYinUtil.getPinYinByString(classificationModel.getName()) + "-" + new Date().getTime();

        classificationModel.setCode(code);

        boolean save = save(classificationModel);
        if(save){
            result.setMsg("添加型号成功！");
        }else{
            result.setMsg("添加型号失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(WmsClassificationModel classificationModel) {

        MyResult result = new MyResult();

        boolean b = updateById(classificationModel);

        if(b){
            result.setMsg("修改型号成功！");
        }else{
            result.setMsg("修改型号失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        String[] idsArr = ids.split(",");

        List<String> list = Arrays.asList(idsArr);

        WmsClassificationModelMapper modelMapper = getBaseMapper();

        int i = modelMapper.deleteBatchIds(list);

        if(i>0){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }


}
